    //
//  Search.m
//  ShoppingCompare
//
//  Created by Wenjie on 11-5-12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LKGoogleTranslator.h"
#import "Search.h"
#import "RootViewController.h"
#import "Result.h"
#import "Taobao.h"
#import "Language.h"
#import "Setup.h"
#import "Register.h"

@implementation Search

- (void) viewDidLoad {
	[super viewDidLoad];
	
	// Navigation set --------------------------------------
	self.title = @"Main Interface";
	self.view.backgroundColor = [UIColor whiteColor];
	
	// First line --------------------------------------
	UILabel* lblLogo = [[[UILabel alloc] initWithFrame:CGRectMake(35.0f, 40.0f, 300.0f, 40.0f)] autorelease];
	[lblLogo setFont:[UIFont systemFontOfSize:28.0]];
	[lblLogo setTextColor:[UIColor blueColor]];
	[lblLogo setBackgroundColor:[UIColor clearColor]];
	lblLogo.text = @"Shopping Compare";

	// Second line --------------------------------------
	UILabel* lblArea = [[[UILabel alloc] initWithFrame:CGRectMake(40.0f, 120.0f, 100.0f, 40.0f)] autorelease];
	[lblArea setFont:[UIFont systemFontOfSize:17.0]];
	lblArea.text = @"IN";
	txtArea = [[[UITextField alloc] initWithFrame:CGRectMake(100.0f, 120.0f, 140.0f, 40.0f)] autorelease];
	txtArea.borderStyle = UITextBorderStyleRoundedRect;
	txtArea.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	txtArea.adjustsFontSizeToFitWidth = YES;
	txtArea.autocorrectionType = UITextAutocorrectionTypeNo;
	txtArea.enabled = NO;
	UIButton* btnArea = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
	btnArea.frame = CGRectMake(240.0f, 120.0f, 40.0f, 40.0f);
	[btnArea addTarget:self action:@selector(selArea) forControlEvents:UIControlEventTouchUpInside];
	
	
	// Third line --------------------------------------
	UILabel* lblKeyword = [[[UILabel alloc] initWithFrame:CGRectMake(40.0f, 190.0f, 100.0f, 40.0f)] autorelease];
	[lblKeyword setFont:[UIFont systemFontOfSize:17.0]];
	lblKeyword.text = @"FIND";
	txtKeyword = [[[UITextField alloc] initWithFrame:CGRectMake(100.0f, 190.0f, 140.0f, 40.0f)] autorelease];
	txtKeyword.borderStyle = UITextBorderStyleRoundedRect;
	txtKeyword.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	txtKeyword.placeholder = @"Close, Food, etc...";
	txtKeyword.adjustsFontSizeToFitWidth = YES;
	txtKeyword.autocorrectionType = UITextAutocorrectionTypeNo;
	txtKeyword.returnKeyType = UIReturnKeyDone;
	txtKeyword.clearButtonMode = UITextFieldViewModeWhileEditing;
	txtKeyword.delegate = self;
	UIButton* btnSetup = [UIButton buttonWithType:UIButtonTypeCustom];
	btnSetup.frame = CGRectMake(250.0f, 200.0f, 20.0f, 20.0f);
	[btnSetup addTarget:self action:@selector(searchDetail) forControlEvents:UIControlEventTouchUpInside];
	[btnSetup setBackgroundImage:[UIImage imageNamed:@"advance.gif"] forState:UIControlStateNormal];
	
	// Forth line --------------------------------------
	UIButton* btnSearch = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btnSearch.frame = CGRectMake(110.0f, 260.0f, 100.0f, 40.0f);
	[btnSearch addTarget:self action:@selector(searchItem) forControlEvents:UIControlEventTouchUpInside];
	[btnSearch setTitle:@"Search" forState:UIControlStateNormal];
	btnSearch.backgroundColor = [UIColor whiteColor];
	
	// Add to view --------------------------------------
	[self.view addSubview:lblLogo];
	[self.view addSubview:lblArea];
	[self.view addSubview:txtArea];
	[self.view addSubview:btnArea];
	[self.view addSubview:lblKeyword];
	[self.view addSubview:txtKeyword];
	[self.view addSubview:btnSetup];
	[self.view addSubview:btnSearch];
	
	// Create plist --------------------------------------
	NSString *documentsPath = [self getDocumentsDirectory];
	
	NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"registerC.plist"];
	NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
	if (plistDict == nil) {
		NSString *errorDesc;
		NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; 
		NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];			
		NSString *documentsPath1 = [self getDocumentsDirectory];
		NSString *savePath = [documentsPath1 stringByAppendingPathComponent:@"registerC.plist"];
		if (plistData) {
			[plistData writeToFile:savePath atomically:YES];
		}
		else {
			[errorDesc release];
		}
	}
	
	NSString* fullPath2 = [documentsPath stringByAppendingPathComponent:@"registerJ.plist"];
	NSMutableDictionary* plistDict2 = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath2];
	if (plistDict2 == nil) {
		NSString* errorDesc2;
		NSMutableDictionary *rootObj2 = [NSMutableDictionary dictionaryWithCapacity:4];
		NSData *plistData2 = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj2 format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc2];
		NSString *documentPath2 = [self getDocumentsDirectory];
		NSString *savePath2 = [documentPath2 stringByAppendingPathComponent:@"registerJ.plist"];
		if (plistData2) {
			[plistData2 writeToFile:savePath2 atomically:YES];
		} else {
			[errorDesc2 release];
		}
	}
	[plistDict release];
	[plistDict2 release];
}

- (void)viewWillAppear:(BOOL)animated {
	if (inTaobao) {
		txtArea.text = @"China";
	} else {
		txtArea.text = @"Japan";
	}
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

// --------------------------------------
- (void)textFieldDidBeginEditing:(UITextField *)textField {
	[UIView beginAnimations:nil context:nil];
	self.view.center = CGPointMake(160, 165);
	[UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
	[UIView beginAnimations:nil context:nil];
	self.view.center = CGPointMake(160, 208);
	[UIView commitAnimations];
}

- (BOOL)textFieldShouldReturn:(UITextField *) textField {
	[textField resignFirstResponder];
	return YES;
}

// --------------------------------------
- (void)selArea {
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	Language* language = [[Language alloc] init];
	[navi pushViewController:language animated:YES];
}

- (void)searchDetail {
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	Setup* setup = [[Setup alloc] init];
	[navi pushViewController:setup animated:YES];
}

- (void)searchItem {
	if (txtKeyword.text.length <= 0) {
		txtKeyword.text = @"insight";
	}
	
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
/*
	LKGoogleTranslator* translator = [[[LKGoogleTranslator alloc] init] autorelease];	
	if (trans) {
		if (inTaobao) {
			txtKeyword.text = [translator translateText: txtKeyword.text fromLanguage: kLKLanguageJapanese toLanguage: kLKLanguageChinese];
		} else {
			txtKeyword.text = [translator translateText: txtKeyword.text fromLanguage: kLKLanguageChinese toLanguage: kLKLanguageJapanese];
		}
	}*/
	//key = [[[NSString alloc] initWithString:txtKeyword.text] autorelease];
	key = [NSString stringWithFormat:@"%@", txtKeyword.text];
	
	
	// ---------------
	LKGoogleTranslator* translator = [[[LKGoogleTranslator alloc] init] autorelease];
	NSString* regi;
	if (trans) {
		if (inTaobao) {
			regi = [Register getKeyFromChina: key];
			if (regi != nil && ![regi isEqualToString:@"(null)"]) {NSLog(@"r%@%d",regi, regi.length);
				key = [NSString stringWithString:regi];
			} else {NSLog(@"k%@",key);
				//NSLog(@"%@~~~%@", key ,[translator translateText: key fromLanguage: kLKLanguageChinese toLanguage: kLKLanguageJapanese]);
				key = [translator translateText: key fromLanguage: kLKLanguageChinese toLanguage: kLKLanguageJapanese];
			}
		} else {
			regi = [Register getKeyFromJapan: key];
			if (regi != nil && ![regi isEqualToString:@"(null)"]) {
				key = [NSString stringWithString:regi];
			} else {
				key = [translator translateText: key fromLanguage: kLKLanguageJapanese toLanguage: kLKLanguageChinese];
			}
		}
	}
	// -------------
	
	
	minP = -1;
	maxP = -1;
	order = 0;
	[Taobao showSearchResult: 1l];
	Result* result = [[Result alloc]init];
	[navi pushViewController:result animated:YES];
}

- (NSString *)getDocumentsDirectory {  
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
	return [paths objectAtIndex:0];  
} 


@end
